const app = getApp()
import {
  upWorkDetail
} from '../../config/https'
//发布 开工图片
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '',     //订单id
    tx: '',           //开工文本
    images: [],       //工地图片
    upArr: []
  },
  getTx(e){
    this.setData({
      tx: e.detail.value
    })
  },
   // 开工图片
   upImg() {
    let that = this
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success: function (res) {
        if (!res.cancel) {
          console.log(res.tapIndex)
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage(type) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      count: 3 - that.data.images.length,
      success: function (res) {
        let imgArr = that.data.images
        res.tempFilePaths.forEach(item => {
          imgArr.push(item)
          wx.uploadFile({
            url: 'https://hsf.0791jr.com/api/Upload/upload',
            filePath: item,
            name: 'file',
            success(res) {
              let upArr = that.data.upArr
              let data = JSON.parse(res.data)
              if (data.code == 1) {
                upArr.push(data.data)
                that.setData({
                  upArr,
                  images: imgArr
                })
              }
            }
          })
        })
      }
    })
  },
  // 删除照片
  imgDelete(e) {
    let that = this
    let imgs = that.data.images,
      index = e.currentTarget.dataset.index;
    imgs.splice(index, 1)
    wx.showModal({
      title: '提示',
      content: '是否删除该图片',
      success(res) {
        if (res.confirm) {
          that.setData({
            images: imgs
          })
        }
      }
    })
  },
  upSubmit(){
    upWorkDetail({
      token: wx.getStorageSync('token'),
      order_id: this.data.order_id,
      content: this.data.tx,
      images: this.data.upArr.join(',')
    }).then(res => {
      app.$tip(res.msg)
      if(res.code == 1){
        wx.navigateBack()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      order_id: options.orderid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})